import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.LinkedList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 * This class is ParentUI
 */
public class ParentUI  extends JPanel {
	/**
	 * save the current panel for the "back" function 
	 */
	private JPanel curPanel;
	/**
	 * combo box message
	 */
	private JComboBox mngBox = null;
	/**
	 * label child  
	 */
	private JLabel mngOptLabel = null;
	/**
	 * title for the current window  
	 */
	private JLabel lblTitle = null;
	/**
	 * select button  
	 */
	private JButton btnSelect = null;
	/**
	 * help button  
	 */
	public JButton btnHelp = null;
	private String ID;
	private Parent P;
	/**
	 * logout button  
	 */
	public JButton btnLogout = null;
	/**
	 * 
	 * @param id
	 */
	public ParentUI(String id) {
		super();
		ID = id;
		P = new Parent(id);
		initialize();
	}
	
	private void initialize() {
		curPanel = this;
		
		mngOptLabel = new JLabel();
		mngOptLabel.setBounds(new Rectangle(55, 71, 48, 19));
		mngOptLabel.setText("Child ID:");
	
		btnSelect = new JButton();
		btnSelect.setBounds(new Rectangle(150, 207, 95, 25));
		btnSelect.setText("Select");

		
		lblTitle = new JLabel("Choose a child data to see");
		lblTitle.setBounds(126, 28, 200, 14);

		
		this.setSize(400, 321);
		this.setLayout(null);
		
		this.add(mngOptLabel);
		this.add(lblTitle);
		this.add(btnSelect(),null);
		this.add(getMngBox(), null);
		btnLogout = new JButton("Logout");        //Logout button
		btnLogout.setBounds(10, 11, 80, 19);
		add(btnLogout);
		btnLogout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				  LogoutWin logt = new LogoutWin();
				  System.out.println("user press Logout button");
			}
		});
		
		btnHelp = new JButton("Help");
		btnHelp.setBounds(390, 11, 80, 19);
		btnHelp.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				  HelpWin help = new HelpWin ("Choose the which child's data you want to see","and click on 'Select' button ");
				  System.out.println("user press Help button");
			}
		});
		this.add(btnHelp,null);
		this.setVisible(true);
		this.setVisible(true);
		
		
	}
	
	private JComboBox getMngBox() {
		if (mngBox == null) {
			mngBox = new JComboBox();
			mngBox.setBounds(new Rectangle(150, 99, 95, 25));
			LinkedList<String> list = P.getChildrens();
			Iterator<String> itr = list.iterator();
			while (itr.hasNext())
				mngBox.addItem(itr.next());

			mngBox.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					System.out.println("itemStateChanged()"); 
				}
			});
		}
		return mngBox;
	}
	

	
	private JButton btnSelect() {
		this.btnSelect.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			 Student S = new Student(mngBox.getSelectedItem().toString());
			 S.DBSHOW();
				}
			});
		return btnSelect;
	}
}